package com.scmt.hmzd.controller;

import java.util.Arrays;
import java.util.Date;

import com.scmt.hmzd.service.IHmOrderItemService;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.core.common.utils.SecurityUtil;
import com.scmt.hmzd.entity.HmOrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scmt.core.common.enums.LogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author
 **/
@RestController
@Api(tags = " 订单详情数据接口")
@RequestMapping("/scmt/hmOrderItem")
public class HmOrderItemController {
    @Autowired
    private IHmOrderItemService hmOrderItemService;
    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 功能描述：新增订单详情数据
     *
     * @param hmOrderItem 实体
     * @return 返回新增结果
     */
    @ApiOperation("新增订单详情数据")
    @PostMapping("addHmOrderItem")
    public Result<Object> addHmOrderItem(@RequestBody HmOrderItem hmOrderItem) {
        try {
            boolean res = hmOrderItemService.save(hmOrderItem);
            if (res) {
                return ResultUtil.data(res, "保存成功");
            } else {
                return ResultUtil.data(res, "保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：更新数据
     *
     * @param hmOrderItem 实体
     * @return 返回更新结果
     */
    @ApiOperation("更新订单详情数据")
    @PostMapping("updateHmOrderItem")
    public Result<Object> updateHmOrderItem(@RequestBody HmOrderItem hmOrderItem) {
        if (hmOrderItem.getId() != null) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderItemService.updateById(hmOrderItem);
            if (res) {
                return ResultUtil.data(res, "修改成功");
            } else {
                return ResultUtil.data(res, "修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("保存异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来删除数据
     *
     * @param ids 主键集合
     * @return 返回删除结果
     */
    @ApiOperation("根据主键来删除订单详情数据")
    @PostMapping("deleteHmOrderItem")
    public Result<Object> deleteHmOrderItem(@RequestParam String[] ids) {
        if (ids == null || ids.length == 0) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            boolean res = hmOrderItemService.removeByIds(Arrays.asList(ids));
            if (res) {
                return ResultUtil.data(res, "删除成功");
            } else {
                return ResultUtil.data(res, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：根据主键来获取数据
     *
     * @param id 主键
     * @return 返回获取结果
     */
    @ApiOperation("根据主键来获取订单详情数据")
    @GetMapping("getHmOrderItem")
    public Result<Object> getHmOrderItem(@RequestParam(name = "id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error("参数为空，请联系管理员！！");
        }
        try {
            HmOrderItem res = hmOrderItemService.getById(id);
            if (res != null) {
                return ResultUtil.data(res, "查询成功");
            } else {
                return ResultUtil.data(res, "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：实现分页查询
     *
     * @param searchVo 需要模糊查询的信息
     * @param pageVo   分页参数
     * @return 返回获取结果
     */
    @ApiOperation("分页查询订单详情数据")
    @GetMapping("queryHmOrderItemList")
    public Result<Object> queryHmOrderItemList(HmOrderItem hmOrderItem, SearchVo searchVo, PageVo pageVo) {
        try {
            IPage<HmOrderItem> result = hmOrderItemService.queryHmOrderItemListByPage(hmOrderItem, searchVo, pageVo);
            return ResultUtil.data(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询异常:" + e.getMessage());
        }
    }

    /**
     * 功能描述：导出数据
     *
     * @param response    请求参数
     * @param hmOrderItem 查询参数
     * @return
     */
    @ApiOperation("导出订单详情数据")
    @PostMapping("/download")
    public void download(HttpServletResponse response, HmOrderItem hmOrderItem) {
        try {
            hmOrderItemService.download(hmOrderItem, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
